package com.yutian.springbootwebflux.config;

import com.yutian.springbootwebflux.base.BaseResult;
import com.yutian.springbootwebflux.exception.CustomException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * 定义全局异常处理，但无法捕获RouterFunctions.route路由的请求异常
 */
@Deprecated
//@RestControllerAdvice
public class CustomExceptionHandler {
    private static final Logger log = LoggerFactory.getLogger(CustomExceptionHandler.class);

    @ExceptionHandler(CustomException.class)
    @ResponseStatus(code = HttpStatus.OK)  // 返回HttpStatus.OK，客户端根据BaseResult.code判断对错
    public BaseResult handleCustomException(CustomException e) {
        log.error(e.getMessage());
        return BaseResult.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(code = HttpStatus.OK)
    public BaseResult handleException(Exception e) {
        log.error(e.getMessage());
        return BaseResult.error("内部错误");
    }
}